<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>rx-login-sdk</title>
  <link rel="stylesheet" href="css/style.css">
  <script src="http://srxboys.cn:8885/dist/index.js" id="rxcornor" link="https://gitee.com/cheere/rxlogin-sdk"
    target="_blank" title="Gitee"></script>
  <script src="./bdlogin.js?1"></script>
</head>

<body>
  <h2>html 原生项目 demo</h2>
  <p> 登录后 - 去签署协议后，并调用当前`页面的方法`、`改变当前dom某个元素值`</p>

  <div>
    <p>PC 浏览器 (window.open)</p>
    <button type="submit" id="btn_submit" onclick="btnAction()">登录</button>
  </div>

  <hr>

  <div>
    <p>微信、qq平台只能用这个</p>
    <button type="submit" id="ifr_submit" onclick="ifrAction()">登录 iframe</button>
  </div>

  <hr>

  <div>
    <p>以上2种，都不支持 iPhone -> UC/夸克</p>
  </div>

  <div id='dialog'>
    <iframe id="ifr" src="" frameborder="0" scrolling="auto" width="100%" height="100%"></iframe>
  </div>

  <hr>
  <div id="cb"></div>


  <br>
  <hr>

  <div>
    <p>下面尝试百度登录方式</p>
    <button type="button" onclick="bdLogin()">去授权登录页</button>
  </div>

  <hr>
  <script>
    var dialogShow = false

    function btnAction() {
      console.log('点击 去登录')
      const href = '/login?aaaa=demo'
      window.open(href, null, " height=500,width=300, Left=300px,Top=20px, menubar=no,titlebar=no,scrollbar=no,toolbar=no, status=no,location=no")
      getLister()
    }

    function ifrAction() {
      dialogShow = true
      document.getElementById('dialog').style.display = 'inherit'
      const href = '/login?type=2&a=1&b=3&c=4&d=5'
      const ifr = document.getElementById('ifr')
      ifr.src = href
      console.log('ifr=', ifr)

      const data = { type: '2', a: '1', b: '3' }
      ifr.contentWindow.postMessage(data)

      getLister()
    }

    function getLister() {
      window.addEventListener('message', cb)
    }

    function disLister() {
      window.removeEventListener('message', cb)
    }


    function cb(msg) {
      console.log('msg=', msg)
      if (msg && typeof msg === 'object') {
        const data = msg.data || ''
        if (data && typeof data === 'object') {
          const devtoolsEnabled = data.devtoolsEnabled
          if (devtoolsEnabled) {
            return
          }
        }
        document.getElementById('cb').innerHTML = '结果: ' + data
      }

      if (dialogShow) {
        dialogShow = false
        document.getElementById('dialog').style.display = 'none'
      }
      setTimeout(() => {
        disLister()
      }, 10);
    }
  </script>
</body>

</html>